package com.example.fpe_vote.db1.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.fpe_vote.common.Result;
import com.example.fpe_vote.db1.entity.Admin;
import com.example.fpe_vote.db1.service.IAdminService;
import com.example.fpe_vote.vo.request.RequestAdminLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAdminService adminService;
    //登录接口
    @PostMapping("/login")
    public Result login(@RequestBody RequestAdminLoginVo vo){
        if(ObjectUtils.isEmpty(vo.getUserName())){
            return Result.getFailure().setMsg("请填写登录名");
        }
        //查询用户是否正确
        Admin admin = adminService.queryBy(vo.getUserName());
        //用户不存在，loginName判断
        if (ObjectUtils.isEmpty(admin)) {
            return Result.getFailure().setMsg("管理员不存在");
        }
        //密码错误
        if (!Objects.equals(admin.getPassWord(),vo.getPassWord())) {
            return Result.getFailure().setMsg("密码错误");
        }
        //用户存在且密码正确
        Map<String,Object> result=new HashMap<>();
        result.put("userName",admin.getUserName());
        return Result.getSuccess().setData(result);
    }
}
